import request from "./request.js"
import qs from 'qs'

//删除商品帖
export const  apiDeleteGoods = (params) => request(`/api/goods/delete/${params}`,'POST',params)
// //获取指定商品帖
export const apiGetaGoods = (params) => request(`/api/goods/get/${params.gid}`,'GET',params)
// //分页查询商品帖 
export const apiPagingQueryGoods = (params) => request(`/api/goods/get/${params.page}/${params.size}`,'GET',params)


// //log
// //拿到所有
export const apiGetAllLog = (params) => request('/api2/log/getAllLog','GET',params)
// //通过userid拿到log
export const  apiGetLogByUserId = (params) => request('/api2/log/getLogByUserId','GET',params)

//搜索
export const  apiGetAllCategories = (params) => request('/api/search/getAllCategories','GET',params)
//根据搜索内容搜索
export const  apiSearchByContent = (params) => request(`/api/search/searchByContent?content=${params.content}&pageNum=${params.pageNum}&pageSize=${params.pageSize}`,'GET',params)



//根据帖子 Id 获取详情
// export const  apiGetPostInfo = (params) => request('/api2/post/getPostInfo','GET',params)
export const  apiGetPostInfo = (params) => request(`/api2/post/getPostInfo?id=${params.id}`,'GET',params)
// //根据帖子分类 Id 分页查询数据
export const  apiGetAll = (params) => request('/api2/post/getAllPosts','GET',params)
//新增帖子
// export const  apInsertPost = (params) => request(`/api2/post/insertPost?title=${params.title}&categoryId=${params.categoryId}&description=${params.description}&postType=${params.postType}&uid=${params.uid}&urlList=${[...params.urlList]}`,'POST',params)
// export const  apInsertPost = (params) => request(`/api2/post/insertPost?${qs.stringify(params)}`,'POST',params)
export const apInsertPost = (params)=>{
 return new Promise((resolve,reject)=>{
  uni.request({
     url: `http://43.138.211.141:9052/api2/post/insertPost`,
     method: 'POST',
	 dataType:'json',
	 data:{
		 ...params,
		 urlList:[...params.urlList]
		 // "urlList":(function(){
			//  let list="["
			// for (var i = 0; i < params.urlList.length; i++) {
			// 	list+=params.urlList[i]
			// 	if(i!=params.urlList.length-1){
			// 		list+=','
			// 	}
			// }
			// list+="]"
			//  return list
		 // }())
	 },
     success(res) {
      resolve(res.data);
     },
     fail(err) {
      reject(err);
     },
     complete() {
      uni.hideLoading();
     }
    });
 })
}
//用户查询个人帖子
export const  apiGetPostByUserId = (params) => request(`/api2/post/getPostByUserId?userId=${params.userId}&pageSize=${params.pageSize}&postType=${params.postType}&pageNum=${params.pageNum}`,'POST',params)
//点击已售出，变成个人已售出帖子
export const  apiDownSoldPost = (params) => request(`/api2/post/downSoldPost?gid=${params.gid}`,'POST',params)




/**
 * 收藏贴相关api
 */
export const apiDeleteFavorite = (params)=>{
 return new Promise((resolve,reject)=>{
  uni.request({
     url: `http://43.138.211.141:9052/api/favorite/delete?`+qs.stringify(params),
     method: 'POST',
	 headers: {
	   'Content-Type': 'application/x-www-form-urlencoded'
	 },
     success(res) {
      resolve(res.data);
     },
     fail(err) {
      reject(err);
     },
     complete() {
      uni.hideLoading();
     }
    });
 })
};

export const apiPageFavorite = (params)=>request(`/api/favorite/page/${params.page}/${params.size}/${params.userId}`,'GET',params)

 // export const apiSaveFavorite = (params)=>request(`/api/favorite/save?userId=${params.userId}&gid=${params.gid}`,'POST',params)
  // export const apiSaveFavorite = (params)=>request(`/api/favorite/save`,'POST',params)

export const apiSaveFavorite =(gid,userId)=>{
 return new Promise((resolve,reject)=>{
  uni.request({
     url: `http://43.138.211.141:9052/api/favorite/${userId}/${gid}`,
     method: 'POST',

     success(res) {
      resolve(res.data);
     },
     fail(err) {
      reject(err);
     },
     complete() {
      
     }
    });
 })
}


export const apiGetFavorite = (params)=>request(`/api/favorite/${params.fid}`,'GET',params)

/**
 * 点赞相关api
 */
export const apiLike =(params)=>request(`/api/like/like/{likeType}`,'POST',params)

export const apiGetAllPosts =(params)=>{
 return new Promise((resolve,reject)=>{
  uni.request({
     url: `http://43.138.211.141:9052/api2/post/getAllPosts?`+qs.stringify(params),
     method: 'GET',
     params:{...params},
     success(res) {
      resolve(res.data);
     },
     fail(err) {
      reject(err);
     },
     complete() {
      uni.hideLoading();
     }
    });
 })
}


//点赞
export const apiIsLike =(params)=>request(`/api2/post/isLiked`,'GET',params)
//对帖子进行点赞
//export const apiToLike =(params)=>request(`/api/like/like?gid=${params.gid}&userId=${params.userId}`,'POST',params)
export const apiToLike =(params)=>{
 return new Promise((resolve,reject)=>{
  uni.request({
     url: `http://43.138.211.141:9052//api/like/like`,
     method: 'POST',
     data:{...params},
     success(res) {
      resolve(res.data);
     },
     fail(err) {
      reject(err);
     },
     complete() {
      uni.hideLoading();
     }
    });
 })
}

//评论
export const apiGetComment =(params)=>request(`/api/comment/list/${params.id}`,'GET',params)

//新增评论
//export const apiAddComment =(params)=>request(`/api/comment/comment?description=${params.description}&gid=${params.gid}&userId=${params.userId}`,'POST',params)
export const apiAddComment =(params)=>{
 return new Promise((resolve,reject)=>{
  uni.request({
     url: `http://43.138.211.141:9052/api/comment/comment`,
     method: 'POST',
     data:{...params},
     success(res) {
      resolve(res.data);
     },
     fail(err) {
      reject(err);
     },
     complete() {
      uni.hideLoading();
     }
    });
 })
}

//表白墙
export const getLovePosts =(params)=>request(`/api2/post/getLovePosts?userId=${params.userId}&pageSize=${params.pageSize}&pageNum=${params.pageNum}`,'POST',params)